var categoryService = require('../../services/categoryService'),
    idService = require('../../services/idService'),
    utils = require('../../helpers/utils'),
    mongoose = require('mongoose'),
    nodeunit = require('nodeunit');

var categoryEg = {
        description: 'misma descripcion'
    },
    originalId,
    updateId,
    removeId;

exports['categories'] = nodeunit.testCase({
    'initialize' : function (test) {
        test.doesNotThrow(
            function() {
                idService.initializeModelId('categories', function(resp) {
                    idService.getId('categories', function(id){
                        originalId = id;
                    });
                });
            }
        );
        setTimeout(test.done, 500);
    },
    'set1' : function (test) {
        test.doesNotThrow(
            function() {
                categoryEg.name = utils.randomString(5);
                categoryService.set(categoryEg, function(category) {
                    test.ok(category, 'Set1 responde');
                    test.ok(category.id, 'Set1 ID insertado');
                    updateId = category.id;
                });
            }
        );
        setTimeout(test.done, 500);
    },
    'set2' : function (test) {
        test.doesNotThrow(
            function() {
                categoryEg.name = utils.randomString(5);
                categoryService.set(categoryEg, function(category) {
                    test.ok(category, 'Set2 responde');
                    test.ok(category.id, 'Set2 ID insertado');
                    removeId = category.id;
                });
            }
        );
        setTimeout(test.done, 500);
    },
    'getAll' : function (test) {
        test.doesNotThrow(
            function() {
                categoryService.getAll(false, function(categories){
                    test.ok(categories, 'Short False: recuperé todas las categorías');
                    categories.forEach(function(category) {
                        test.ok(category, 'Short False: categoría OK');
                        test.ok(category.id, 'Short False: ID recuperado');
                        test.ok(category.name, 'Short False: Nombre recuperado');
                        test.ok(category.description, 'Short False: descripción recuperada');
                    });
                    categoryService.getAll(true, function(categories){
                        test.ok(categories, 'Short True: recuperé todas las categorías');
                        categories.forEach(function(category) {
                            test.ok(category, 'Short True: categoría OK');
                            test.ok(category.id, 'Short True: ID recuperado');
                            test.ok(category.name, 'Short True: Nombre recuperado');
                            //test.equal(category.description, false, 'descripción no recuperada');
                        });
                    });
                });
            }
        );
        setTimeout(test.done, 1000);
    },
    'find' : function (test) {
        test.doesNotThrow(
            function() {
                categoryService.find({description: categoryEg.description}, function(categories){
                    test.ok(categories, 'recuperé categorías');
                    test.equal(categories.length, 2, 'Recupere las 2 categorías');
                    categories.forEach(function(category) {
                        test.equal(category.description, categoryEg.description, 'description OK');
                    });
                });
            }
        );
        setTimeout(test.done, 1000);
    },
    'findOne' : function (test) {
        test.doesNotThrow(
            function() {
                categoryService.findOne({name: categoryEg.name}, function(category){
                    test.ok(category, 'recuperé la categoría insertada');
                    test.ok(category.id, 'Id autogenerado: ' + category.id);
                    test.equal(category.name, categoryEg.name, 'name OK');
                    test.equal(category.description, categoryEg.description, 'description OK');
                });
            }
        );
        setTimeout(test.done, 1000);
    },
    'update' : function (test) {
        test.doesNotThrow(
            function() {
                categoryEg.id = updateId;
                categoryEg.name = utils.randomString(5);
                categoryEg.description = utils.randomString(8);
                categoryService.update(categoryEg.id, categoryEg, function(resp) {
                    categoryService.findOne({id: categoryEg.id}, function(category) {
                        test.ok(category, 'Recupere categoría actualizada');
                        test.equal(category.name, categoryEg.name, 'Nombre actualizado');
                        test.equal(category.description, categoryEg.description, 'Descripción actualizad1');
                    });
                });
            }
        );
        setTimeout(test.done, 1000);
    },
    'remove' : function (test) {
        test.doesNotThrow(
            function() {
                categoryService.remove(updateId, function(resp) {
                    categoryService.findOne({id: updateId}, function(category) {
                        test.equal(category, null, 'Categoría eliminada');
                    });
                });
            }
        );
        setTimeout(test.done, 1000);
    },
    'done': function(test) {
        categoryService.remove(removeId, function(resp) {});
        idService.upsert('categories', {$set: { nextId: originalId} }, function(){
                mongoose.disconnect();
                test.done();
        });
    }
});
